package va_btree;

import btree.*;
import bufmgr.*;
import global.*;
import heap.*;
import index.IndexException;
import iterator.*;
import va.BaseRSIndexScan;
import va.BitSet;
import va.Range;
import va.VAEntry;
import va_file.VAFile;

import java.io.IOException;

public class RSBTIndexScan extends BaseRSIndexScan {

    public RSBTIndexScan(IndexType index,
                              String relName,
                              String indexName,
                              AttrType[] types,
                              short[] str_sizes,
                              int noInFields,
                              int noOutFields,
                              FldSpec[] outFields,
                              CondExpr[] selects,
                              int fldNum,
                              Vector100Dtype target,
                              int distance,
                              int noOfBits) throws IndexException, HFException, HFBufMgrException, HFDiskMgrException, IOException, InvalidTupleSizeException,
            InvalidTypeException, FileScanException, TupleUtilsException, InvalidRelation, JoinsException, PageNotReadException, PredEvalException,
            UnknowAttrType, FieldNumberOutOfBoundException, WrongPermat, GetFileEntryException, ConstructPageException, AddFileEntryException,
            KeyNotMatchException, IteratorException, PinPageException, UnpinPageException, ScanIteratorException, PageUnpinnedException, InvalidFrameNumberException, HashEntryNotFoundException, ReplacerException {

        //setRIDs(index, indexName, target, distance);

        super(index, relName, indexName, types, str_sizes, noInFields, noOutFields, outFields, selects, fldNum, target, distance, noOfBits);

        VABTreeFile file = new VABTreeFile(indexName, noOfBits);
        VABTreeScan scan = new VABTreeScan(file);

        //Get the initial strings for approximation
        String lowKey, highKey;
        Range[] ranges = VAFile.GetDefaultRanges(noOfBits);
        BitSet approx = VABTreeFile.GetMinimumApproximation(target, ranges, distance, this.noOfBits, new BitSet());
        lowKey = VAEntry.GetApproximationString(approx, this.noOfBits);

        approx = VABTreeFile.GetMaximumApproximation(target, ranges, distance, this.noOfBits, new BitSet());
        highKey = VAEntry.GetApproximationString(approx, this.noOfBits);

        //Use the scan object to get the RIDs
        this.rids = scan.VABTreeRangeScan(new StringKey(lowKey), new StringKey(highKey), target, distance);
        scan.close();
        file.close();
    }

}
